package com.wl.eduorder.service.impl;

import com.wl.connonutils.R;
import com.wl.connonutils.ordervo.CourseWebVoOrder;
import com.wl.connonutils.ordervo.MemberOrder;
import com.wl.eduorder.client.CourseClient;
import com.wl.eduorder.client.MemberClient;
import com.wl.eduorder.entity.Member;
import com.wl.eduorder.entity.Order;
import com.wl.eduorder.entity.vo.CourseInfoVo;
import com.wl.eduorder.mapper.OrderMapper;
import com.wl.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.IdGenerator;

import java.util.Date;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author wl
 * @since 2021-09-06
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private MemberClient memberClient;

    @Override
    public String createOrders(String id, String memberIdByJwtToken) {
        //远程调用获取用户信息
        MemberOrder userInfoOrder = memberClient.getMemberInfoById(memberIdByJwtToken);
        //远程调用获取课程信息
        CourseWebVoOrder courseInfoOrder = courseClient.getcourseInfoOrder(id);
        Order order = new Order();
        String orderNo = OrderNoUtil.getOrderNo();
        order.setOrderNo(orderNo);
        order.setCourseId(id);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setGmtCreate(new Date());
        order.setGmtModified(new Date());
        order.setStatus(0);  //订单状态（0：未支付 1：已支付）
        order.setPayType(1);  //支付类型 ，微信1
        this.save(order);
        return order.getOrderNo();
    }
}
